<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * 打印机列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['print_types']=array('0'=>'下单打印','1'=>'付款打印');
$return['print_alls']=array('0'=>'全单打印','1'=>'分单打印');
$return['printers'] = $this->plugin('jy_print')->getlist();
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['store_id']=$store_id;
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_qiuyuyue_printer',$params,array($pindex,$psize),$total,$fields,'','id desc');
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'打印机列表'),
			array('op'=>'edit','title'=>'添加打印机'),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'title','title'=>'打印机名称','type'=>'string'),
			array('name'=>'print_status','title'=>'是否开启','type'=>'switch','set'=>'set'),
			array('name'=>'is_meal','title'=>'是否店内服务','type'=>'switch','set'=>'set'),
			array('name'=>'is_delivery','title'=>'是否上门服务','type'=>'switch','set'=>'set'),
			array('name'=>'is_reservation','title'=>'是否支持预定','type'=>'switch','set'=>'set'),
			array('name'=>'is_shouyin','title'=>'是否支持收银','type'=>'switch','set'=>'set'),
			array('name'=>'print_type','title'=>'打印类型','type'=>'radio','options'=>'print_types'),
		)),
		'search_one'=>array('name'=>'title'),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择打印机！','error');
	}
	$update=array('store_id'=>$store_id);
	$post=get_postdata();
	if(isset($post['print_status'])){
		$update['print_status']=intval($post['print_status']);
	}elseif(isset($post['is_meal'])){
		$update['is_meal']=intval($post['is_meal']);
	}elseif(isset($post['is_delivery'])){
		$update['is_delivery']=intval($post['is_delivery']);
	}elseif(isset($post['is_snack'])){
		$update['is_snack']=intval($post['is_snack']);
	}elseif(isset($post['is_reservation'])){
		$update['is_reservation']=intval($post['is_reservation']);
	}elseif(isset($post['is_shouyin'])){
		$update['is_shouyin']=intval($post['is_shouyin']);
	}
	if($update){
		pdo_update('jy_qiuyuyue_printer',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_qiuyuyue_printer',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回打印机列表'),
		),
		'edits'=>array(
			array(
				'title'=>'打印机',
				'list'=>array(
					array('name'=>'title','title'=>'打印机名称','type'=>'string'),
					array('name'=>'printerid','title'=>'选择打印机','type'=>'select','options'=>'printers','help'=>'如果没有打印机，请移步“进云打印机”应用，添加打印机！'),
					array('name'=>'print_status','title'=>'是否开启','type'=>'switch','set'=>'set'),
					array('name'=>'is_meal','title'=>'是否店内点餐','type'=>'switch','set'=>'set'),
					array('name'=>'is_delivery','title'=>'是否外卖订餐','type'=>'switch','set'=>'set'),
					array('name'=>'is_reservation','title'=>'是否支持预定','type'=>'switch','set'=>'set'),
					array('name'=>'is_shouyin','title'=>'是否支持收银','type'=>'switch','set'=>'set'),
					array('name'=>'print_type','title'=>'打印类型','type'=>'radio','options'=>'print_types'),
					array('name'=>'goods_groupid','title'=>'打印商品分类','type'=>'select','options'=>'goods_groups'),
					array('name'=>'print_top','title'=>'头部内容','type'=>'string'),
					array('name'=>'print_bottom','title'=>'底部内容','type'=>'string'),
					array('name'=>'qrcode_status','title'=>'是否打印机二维码','type'=>'switch'),
					array('name'=>'qrcode_url','title'=>'二维码地址','type'=>'string','show'=>array('qrcode_status'=>1)),
					array('name'=>'is_print_all','title'=>'全单打印？','type'=>'radio','options'=>'print_alls'),
				)
			),
		)
	);
	$return['htmls']=$htmls;
	$params['uniacid']=$_SESSION['uniacid'];
	$params['store_id']=$store_id;
	$return['goods_groups']=pdo_getall('jy_qiuyuyue_goods_group',$params);
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		'is_meal'=>intval($postdata['is_meal']),
		'print_status'=>intval($postdata['print_status']),
		'is_reservation'=>intval($postdata['is_reservation']),
		'is_delivery'=>intval($postdata['is_delivery']),
		'is_snack'=>intval($postdata['is_snack']),
		'is_shouyin'=>intval($postdata['is_shouyin']),
		'print_type'=>intval($postdata['print_type']),
		'print_top'=>trim($postdata['print_top']),
		'print_bottom'=>trim($postdata['print_bottom']),
		'qrcode_status'=>intval($postdata['qrcode_status']),
		'qrcode_url'=>trim($postdata['qrcode_url']),
		'is_print_all'=>intval($postdata['is_print_all']),
		'printerid'=>intval($postdata['printerid']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_qiuyuyue_printer',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['store_id']=$store_id;
		pdo_insert('jy_qiuyuyue_printer',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_qiuyuyue_printer',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}